<template>
  <div class="addEdit-block">
    <el-form
        class="detail-form-content"
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        style="background-color: #ffffff;"
    >
      <!-- 基本信息 -->
      <div class="section-header">
        <h3>基本信息</h3>
      </div>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item class="input" v-if="type!='info'" label="食谱名称" prop="shipumingcheng">
            <el-input v-model="ruleForm.shipumingcheng"
                      placeholder="请输入食谱名称" clearable :readonly="ro.shipumingcheng"></el-input>
          </el-form-item>
          <div v-else>
            <el-form-item class="input" label="食谱名称" prop="shipumingcheng">
              <el-input v-model="ruleForm.shipumingcheng"
                        placeholder="食谱名称" readonly></el-input>
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <el-form-item class="select" v-if="type!='info'" label="职业类型" prop="zhiyeleixing">
            <el-select :disabled="ro.zhiyeleixing" v-model="ruleForm.zhiyeleixing" placeholder="请选择职业类型">
              <el-option
                  v-for="(item,index) in zhiyeleixingOptions"
                  v-bind:key="index"
                  :label="item"
                  :value="item">
              </el-option>
            </el-select>
          </el-form-item>
          <div v-else>
            <el-form-item class="input" label="职业类型" prop="zhiyeleixing">
              <el-input v-model="ruleForm.zhiyeleixing"
                        placeholder="职业类型" readonly></el-input>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      
      <!-- 付费设置 -->
      <div class="section-header">
        <h3>付费设置</h3>
      </div>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item class="select" v-if="type!='info'" label="是否付费" prop="ispay">
            <el-select v-model="ruleForm.ispay" placeholder="请选择是否付费">
              <el-option
                  :label="'是'"
                  :value="1">
              </el-option>
              <el-option
                  :label="'否'"
                  :value="0">
              </el-option>
            </el-select>
          </el-form-item>
          <div v-else>
            <el-form-item class="input" label="是否付费" prop="ispay">
                <el-input v-model="ispayText"
                  placeholder="是否付费" readonly></el-input>
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <el-form-item class="input" v-if="type!='info' && ruleForm.ispay == 1" label="价格(元)" prop="price">
            <el-input-number v-model="ruleForm.price" :precision="2" :step="0.1" :min="0"
                placeholder="请输入价格" clearable :readonly="ro.price" controls-position="right"></el-input-number>
          </el-form-item>
          <div v-else-if="type=='info' && ruleForm.ispay == 1">
            <el-form-item class="input" label="价格(元)" prop="price">
                <el-input v-model="ruleForm.price"
                  placeholder="价格" readonly></el-input>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      
      <!-- 食谱封面 -->
      <div class="section-header">
        <h3>食谱封面</h3>
      </div>
      <el-row>
        <el-col :span="24">
          <el-form-item class="upload" v-if="type!='info' && !ro.shipufengmian" label="食谱封面" prop="shipufengmian">
            <file-upload
                tip="点击上传食谱封面"
                action="file/upload"
                :limit="3"
                :multiple="true"
                :fileUrls="ruleForm.shipufengmian?ruleForm.shipufengmian:''"
                @change="shipufengmianUploadChange"
            ></file-upload>
          </el-form-item>
          <div v-else>
            <el-form-item v-if="ruleForm.shipufengmian" label="食谱封面" prop="shipufengmian">
              <img style="margin-right:20px;" v-bind:key="index"
                   v-for="(item,index) in ruleForm.shipufengmian.split(',')" :src="$base.url+item" width="100"
                   height="100">
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      
      <!-- 食谱详情 -->
      <div class="section-header">
        <h3>食谱详情</h3>
      </div>
      <el-row>
        <el-col :span="24">
          <el-form-item class="textarea" v-if="type!='info'" label="简介" prop="jianjie">
            <el-input
                style="min-width: 200px; max-width: 100%;"
                type="textarea"
                :rows="5"
                placeholder="简要描述食谱的特点和适用人群"
                v-model="ruleForm.jianjie">
            </el-input>
          </el-form-item>
          <div v-else>
            <el-form-item v-if="ruleForm.jianjie" label="简介" prop="jianjie">
              <span>{{ ruleForm.jianjie }}</span>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item v-if="type!='info'" label="功效介绍" prop="gongxiaojieshao">
            <editor
                style="min-width: 200px; max-width: 100%;"
                v-model="ruleForm.gongxiaojieshao"
                class="editor"
                action="file/upload">
            </editor>
          </el-form-item>
          <div v-else>
            <el-form-item v-if="ruleForm.gongxiaojieshao" label="功效介绍" prop="gongxiaojieshao">
              <span v-html="ruleForm.gongxiaojieshao"></span>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item v-if="type!='info'" label="做法介绍" prop="zuofajieshao">
            <editor
                style="min-width: 200px; max-width: 100%;"
                v-model="ruleForm.zuofajieshao"
                class="editor"
                action="file/upload">
            </editor>
          </el-form-item>
          <div v-else>
            <el-form-item v-if="ruleForm.zuofajieshao" label="做法介绍" prop="zuofajieshao">
              <span v-html="ruleForm.zuofajieshao"></span>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      
      <!-- 操作按钮 -->
      <el-form-item class="btn">
        <el-button v-if="type!=='info'" type="primary" class="btn-success" @click="onSubmit">提交</el-button>
        <el-button v-if="type!=='info'" class="btn-close" @click="back()">取消</el-button>
        <el-button v-if="type==='info'" class="btn-close" @click="back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
// 数字，邮件，手机，url，身份证校验
import {isNumber, isIntNumer, isEmail, isPhone, isMobile, isURL, checkIdCard} from "@/utils/validate";

export default {
  data() {
    var validateIntNumber = (rule, value, callback) => {
      if (!value) {
        callback();
      } else if (!isIntNumer(value)) {
        callback(new Error("请输入整数"));
      } else {
        callback();
      }
    };
    var validatePrice = (rule, value, callback) => {
      if (this.ruleForm.ispay == 1 && (!value || value <= 0)) {
        callback(new Error("付费食谱必须设置价格且价格必须大于0"));
      } else {
        callback();
      }
    };
    return {
      id: '',
      type: '',
      ro: {
        shipumingcheng: false,
        zhiyeleixing: false,
        shipufengmian: false,
        jianjie: false,
        gongxiaojieshao: false,
        zuofajieshao: false,
        clicktime: false,
        clicknum: false,
        ispay: false,
        price: false,
      },
      ruleForm: {
        shipumingcheng: '',
        zhiyeleixing: '',
        shipufengmian: '',
        jianjie: '',
        gongxiaojieshao: '',
        zuofajieshao: '',
        clicktime: '',
        ispay: 0,
        price: 0,
      },
      zhiyeleixingOptions: [],
      rules: {
        shipumingcheng: [
          { required: true, message: '食谱名称不能为空', trigger: 'blur' },
        ],
        zhiyeleixing: [
          { required: true, message: '职业类型不能为空', trigger: 'change' },
        ],
        shipufengmian: [
          { required: true, message: '食谱封面不能为空', trigger: 'blur' },
        ],
        jianjie: [
          { required: true, message: '简介不能为空', trigger: 'blur' },
        ],
        ispay: [
          { required: true, message: '请选择是否付费', trigger: 'change' },
        ],
        price: [
          { validator: validatePrice, trigger: 'blur' },
        ],
        gongxiaojieshao: [],
        zuofajieshao: [],
        clicktime: [],
        clicknum: [
          { validator: validateIntNumber, trigger: 'blur' },
        ]
      }
    };
  },
  props: ["parent"],
  computed: {
    ispayText() {
      return this.ruleForm.ispay == 1 ? '是' : '否';
    }
  },
  created() {
    this.getZhiyeleixingOptions();
  },
  methods: {
    // 下载
    download(file) {
      window.open(`${file}`)
    },
    // 初始化
    init(id, type) {
      if (id) {
        this.id = id;
        this.type = type;
      }
      if (this.type === 'info' || this.type === 'else') {
        this.info(id);
      } else if (this.type == 'logistics') {
        this.logistics = false;
        this.info(id);
      }
    },
    // 获取职业类型选项
    getZhiyeleixingOptions() {
      this.$http({
        url: `option/zhiyeleixing/zhiyeleixing`,
        method: "get"
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.zhiyeleixingOptions = data.data;
        }
      });
    },
    // 多级联动参数
    info(id) {
      this.$http({
        url: `zhiyeshipu/info/${id}`,
        method: "get"
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.ruleForm = data.data;
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    // 提交
    onSubmit() {
      if(this.ruleForm.shipufengmian!=null) {
        this.ruleForm.shipufengmian = this.ruleForm.shipufengmian.replace(new RegExp(this.$base.url,"g"),"");
      }
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: `zhiyeshipu/${!this.ruleForm.id ? "save" : "update"}`,
            method: "post",
            data: this.ruleForm
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.parent.showFlag = true;
                  this.parent.addOrUpdateFlag = false;
                  this.parent.search();
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 返回
    back() {
      this.parent.showFlag = true;
      this.parent.addOrUpdateFlag = false;
    },
    shipufengmianUploadChange(fileUrls) {
      this.ruleForm.shipufengmian = fileUrls;
      this.ro.shipufengmian = false;
    }
  }
};
</script>
<style lang="scss" scoped>
.addEdit-block {
  margin: auto;
  width: 100%;
  padding: 20px;
  
  .section-header {
    border-left: 4px solid #4a90e2;
    padding-left: 10px;
    margin: 20px 0 15px 0;
    
    h3 {
      font-size: 16px;
      font-weight: 500;
      color: #303133;
      margin: 0;
    }
  }
  
  .detail-form-content {
    padding: 20px;
    border-radius: 6px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
    
    .el-form-item {
      margin-bottom: 20px;
    }
    
    .el-input-number {
      width: 100%;
    }
  }
  
  .btn {
    text-align: center;
    margin-top: 30px;

    .btn-success {
      padding: 12px 25px;
      background-color: #4a90e2;
      border-color: #4a90e2;
      color: #ffffff;
      margin-right: 10px;
      font-size: 14px;
    }
    
    .btn-close {
      padding: 12px 25px;
      border-color: #DCDFE6;
      color: #606266;
      font-size: 14px;
    }
  }
  
  .editor {
    height: 400px;
    margin-bottom: 20px;
  }
  
  .el-row {
    margin-bottom: 10px;
  }
}
</style>
